home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / cli / master60.zoo / master / tools / readhist / readhist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-18  |  1.5 KB  |  74 lines

  1. #include    <stdio.h>
  2. #include    <stat.h>
  3. #include    <tos.h>
  4. #define    CASE    break;case
  5.  
  6. extern    int histinsert(const char *filename);
  7. extern    int main(int argc,char **argv);
  8.  
  9. int
  10. histinsert(filename)
  11. const char *filename;
  12. {
  13.   FILE *fp;
  14.   char *fm;
  15.   char *fme;
  16.   struct stat statbuf ;
  17.   size_t totalsize;
  18.   void    *memstart;
  19.  
  20. #ifdef    __TURBOC__
  21.   /* this code is here to get register D4 saved, because stat()
  22.      in the turbo-c-library does modify it without saving */
  23.   int i1,i2,i3,i4;
  24.   i1=1;
  25.   i2=1;
  26.   i3=1;
  27.   i4=i1+i2+i3;
  28. #endif    __TURBOC__
  29.  
  30.   if (fp = fopen(filename,"r")) {
  31.     stat(filename,&statbuf);
  32.     if ((totalsize = coreleft()) > statbuf.st_size) {
  33.       if (memstart = fm = (char*) Malloc(totalsize)) {
  34.     fme = fm+fread(fm,sizeof(char),statbuf.st_size,fp);
  35.     *fme++ = '\0'; /* for files, ending in an unterminated line */
  36.     while (fm < fme) {
  37.       if (*fm == '\n' || *fm == '\r') *(fm)='\016';
  38.       fm++;
  39.     }
  40.     Mshrink(0,memstart,(size_t)fme-(size_t)memstart);
  41.     fclose(fp);
  42.     csystem(memstart);
  43.     return 0;
  44.       } else {
  45.     fclose(fp);
  46.     return 2;
  47.       }
  48.     } else {
  49.       fclose(fp);
  50.       return 3;
  51.     }
  52.   } else {
  53.     return 4;
  54.   }
  55. }
  56.  
  57. int
  58. main(argc,argv)
  59. int argc;
  60. char **argv;
  61. {
  62.   if (argc > 1) {
  63.     switch (histinsert(argv[1])) {
  64.       CASE 2:
  65.     fprintf(stderr,"couldn't allocate memory\n");
  66.       CASE 3:
  67.     fprintf(stderr,"not enough memory available\n");
  68.       CASE 4:
  69.     fprintf(stderr,"couldn't open file %s\n",argv[1]);
  70.     }
  71.   }
  72.   return 0;
  73. }
  74.